/*
 * File: bms_balance.c
 *
 * Code generated for Simulink model 'bms_balance'.
 *
 * Model version                  : 1.71
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Thu Sep 10 18:24:53 2020
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: STMicroelectronics->ST10/Super10
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "bms_balance.h"
#include "bms_balance_private.h"


/* Imported (extern) block signals */
real_T MinCellVolt;             /* '<Root>/MinCellVolt' */
boolean_T BalModeCondition;     /* '<Root>/BalModeCondition' */
/* Block signals (auto storage) */
B_bms_balance_T bms_balance_B;

/* Block states (auto storage) */
DW_bms_balance_T bms_balance_DW;

/* Real-time model */
RT_MODEL_bms_balance_T bms_balance_M_;
RT_MODEL_bms_balance_T *const bms_balance_M = &bms_balance_M_;
uint16_T rtb_VltgRecv[64];
/* Model step function */
void bms_balance_step(void)
{
  /* local block i/o variables */

  uint8_T b_index;
  boolean_T rtb_LogicalOperator;
  uint16_T qY;

  /* S-Function (VltgRecv): '<Root>/VltgRecv' */
//  VltgRecv(&rtb_VltgRecv[0]);

  /* Logic: '<Root>/Logical Operator' incorporates:
   *  Constant: '<Root>/Constant12'
   *  Constant: '<Root>/Constant3'
   *  Inport: '<Root>/MinCellVolt'
   *  RelationalOperator: '<Root>/Relational Operator2'
   *  RelationalOperator: '<Root>/Relational Operator3'
   */
  rtb_LogicalOperator = ((MinCellVolt <= 4200.0) && (MinCellVolt >= 3500.0));

  /* Chart: '<Root>/UnbalanceJudge' incorporates:
   *  Constant: '<Root>/Constant'
   *  Constant: '<Root>/Constant1'
   *  Constant: '<Root>/Constant2'
   *  Constant: '<Root>/Constant5'
   *  Constant: '<Root>/Constant6'
   *  Constant: '<Root>/Constant7'
   *  Constant: '<Root>/Constant8'
   *  Inport: '<Root>/BalModeCondition'
   *  Inport: '<Root>/MinCellVolt'
   */
  /* Gateway: UnbalanceJudge */
  /* During: UnbalanceJudge */
  /* Entry Internal: UnbalanceJudge */
  /* Transition: '<S1>:1' */
  /* Transition: '<S1>:4' */
  for (b_index = 0U; b_index < 54; b_index++) {
    /* Transition: '<S1>:2' */
    if (((real_T)rtb_VltgRecv[b_index] - MinCellVolt > 10.0) &&
        (rtb_VltgRecv[b_index] < MAX_uint16_T) && (rtb_VltgRecv[b_index] > 0U))
    {
      /* Transition: '<S1>:13' */
      /* Transition: '<S1>:52' */
      bms_balance_DW.CellBalEn[b_index] = 1U;
    } else {
      /* Transition: '<S1>:3' */
    }

    if (((real_T)rtb_VltgRecv[b_index] - MinCellVolt <= 5.0) &&
        (rtb_VltgRecv[b_index] < MAX_uint16_T)) {
      /* Transition: '<S1>:98' */
      /* Transition: '<S1>:100' */
      bms_balance_DW.CellBalEn[b_index] = 0U;
    } else {
      /* Transition: '<S1>:101' */
    }

    if ((bms_balance_DW.CellBalEn[b_index] != 0U) && bms_balance_DW.init[b_index])
    {
      /* Transition: '<S1>:37' */
      /* Transition: '<S1>:31' */
      bms_balance_DW.init[b_index] = false;
      bms_balance_DW.delay_step[b_index] = 401U;

      /* Transition: '<S1>:38' */
    } else {
      /* Transition: '<S1>:46' */
    }

    /* Transition: '<S1>:39' */
    if (bms_balance_DW.delay_step[b_index] > 0U) {
      /* Transition: '<S1>:48' */
      /* Transition: '<S1>:47' */
      qY = bms_balance_DW.delay_step[b_index] - /*MW:OvSatOk*/ 1U;
      if (qY > bms_balance_DW.delay_step[b_index]) {
        qY = 0U;
      }

      bms_balance_DW.delay_step[b_index] = qY;

      /* Transition: '<S1>:32' */
    } else {
      /* Transition: '<S1>:49' */
    }

    /* Transition: '<S1>:36' */
    bms_balance_DW.CellBalEn[b_index] = (uint16_T)
      ((bms_balance_DW.CellBalEn[b_index] != 0U) &&
       (bms_balance_DW.delay_step[b_index] <= 0U));
    bms_balance_DW.CellBalEn[b_index] = (uint16_T)
      ((bms_balance_DW.CellBalEn[b_index] != 0U) && rtb_LogicalOperator &&
       BalModeCondition);

    /* Transition: '<S1>:5' */
  }

  /* Transition: '<S1>:6' */
  /* Transition: '<S1>:74' */
  for (b_index = 0U; b_index < 54; b_index++) {
    /* Transition: '<S1>:82' */
    /* Transition: '<S1>:80' */
    qY = bms_balance_DW.counter[b_index] + /*MW:OvSatOk*/ 1U;
    if (qY < bms_balance_DW.counter[b_index]) {
      qY = MAX_uint16_T;
    }

    bms_balance_DW.counter[b_index] = qY;
    if (bms_balance_DW.counter[b_index] > 333U) {
      /* Transition: '<S1>:78' */
      /* Transition: '<S1>:79' */
      bms_balance_DW.out[b_index] = 0U;

      /* Transition: '<S1>:65' */
    } else {
      /* Transition: '<S1>:66' */
      bms_balance_DW.out[b_index] = 1U;
    }

    /* Transition: '<S1>:62' */
    bms_balance_B.BMS_BalanceEn[b_index] = (uint16_T)
      ((bms_balance_DW.CellBalEn[b_index] != 0U) && (bms_balance_DW.out[b_index]
        != 0));
    if (bms_balance_DW.counter[b_index] >= 500U) {
      /* Transition: '<S1>:81' */
      /* Transition: '<S1>:84' */
      bms_balance_DW.counter[b_index] = 0U;

      /* Transition: '<S1>:71' */
    } else {
      /* Transition: '<S1>:70' */
    }

    /* Transition: '<S1>:75' */
  }

  /* End of Chart: '<Root>/UnbalanceJudge' */
  /* Transition: '<S1>:67' */

  /* S-Function (BalEnaSend): '<Root>/BalEnaSend' */
//  BalEnaSend((uint16_T*)&bms_balance_B.BMS_BalanceEn[0]);
}

/* Model initialize function */
void bms_balance_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(bms_balance_M, (NULL));

  /* block I/O */
  (void) memset(((void *) &bms_balance_B), 0,
                sizeof(B_bms_balance_T));

  /* states (dwork) */
  (void) memset((void *)&bms_balance_DW, 0,
                sizeof(DW_bms_balance_T));

  {
    int16_T i;

    /* SystemInitialize for Chart: '<Root>/UnbalanceJudge' */
    for (i = 0; i < 54; i++) {
      bms_balance_DW.delay_step[i] = 0U;
      bms_balance_DW.init[i] = true;
      bms_balance_DW.counter[i] = 0U;
      bms_balance_DW.out[i] = 0U;
      bms_balance_DW.CellBalEn[i] = 0U;
      bms_balance_B.BMS_BalanceEn[i] = 0U;
    }

    /* End of SystemInitialize for Chart: '<Root>/UnbalanceJudge' */
  }
}

/* Model terminate function */
void bms_balance_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
